package com.xilihui.xlh.business.activitys;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.sax.StartElementListener;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.adapters.AppointmentAdapter;
import com.xilihui.xlh.business.adapters.BasePagerAdapter;
import com.xilihui.xlh.business.fragments.TakeGoodsFragment;
import com.xilihui.xlh.business.fragments.WeAppointmentFragment;
import com.xilihui.xlh.component.recyclerview.BaseAdapter;
import com.xilihui.xlh.component.recyclerview.BaseViewHolder;
import com.xilihui.xlh.constant.StrConst;
import com.xilihui.xlh.core.app.BaseCompatActivity;
import com.xilihui.xlh.core.app.ToolBaseCompatActivity;
import com.xilihui.xlh.core.util.LogUtil;

import java.lang.reflect.Field;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/5/17.
 */

public class AppointmentActivity extends ToolBaseCompatActivity{
    
    @BindView(R.id.tablayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    
    @Override
    public int getLayout() {
        return R.layout.activity_apponitment;
    }

    @OnClick(R.id.iv_toolbar_left)
    public void back() {
        finish();
    }
    
    @OnClick(R.id.tv_toolbar_right)
    public void history(){
        startActivity(new Intent(this,HistoryRecordActivity.class));
    }
    
    @Override
    public String initTitle() {
        return null;
    }

    @Override
    public void initUI(@Nullable Bundle savedInstanceState) {
        setToolTitle("我的预约");
        setToolRightText("历史记录");

        ArrayList<String> titles = new ArrayList<>();
        titles.add("待开始");
        titles.add("待结束");
        titles.add("待收款");


        BasePagerAdapter basePagerAdapter = new BasePagerAdapter(getSupportFragmentManager(),
                this,titles);
        basePagerAdapter.setItemFragment(new BasePagerAdapter.getItemFragment() {
            @Override
            public Fragment getItem(int position) {
                WeAppointmentFragment fragment = new WeAppointmentFragment();
                Bundle bundle = new Bundle();
                bundle.putInt(StrConst.MY_APPOINT_TYPE,position);
                fragment.setArguments(bundle);
                return fragment;
            }
        });
        
        viewPager.setAdapter(basePagerAdapter);
        viewPager.setOffscreenPageLimit(2);
        tabLayout.setupWithViewPager(viewPager);
        setIndicator(tabLayout,20,20);
        
        
    }

    @Override
    public void initData() {
        
    }

    public static void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        tabStrip.setAccessible(true);
        LinearLayout llTab = null;
        try {
            llTab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());

        for (int i = 0; i < llTab.getChildCount(); i++) {
            View child = llTab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }
    }
    
    
}
